﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sales.Data.Access.Persistence;
using Sales.Data.Access.Entities;

namespace Sales.UI.Win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cliente _Cliente = new Cliente();

            _Cliente.Id = (textBox1.Text == "") ? 0 : int.Parse(textBox1.Text);
            _Cliente.Nome = textBox2.Text;
            _Cliente.RG = textBox3.Text;
            _Cliente.CPF = textBox4.Text;

            ClienteService _ClienteService = new ClienteService();

            if (textBox1.Text != "")
            {
                _ClienteService.ExcluiCliente(_Cliente);
                textBox1.Text = "";
                Form1_Load(sender, e);
            }
            else
            {
                _ClienteService.AdicionaCliente(_Cliente);
                dataGridView1.DataSource = _ClienteService.BuscaCliente(_Cliente.Id);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ClienteService _ClienteService = new ClienteService();

            dataGridView1.DataSource = _ClienteService.ListarClientes();
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow rowGrid = dataGridView1.SelectedRows[0];
                textBox1.Text = rowGrid.Cells[0].Value.ToString();
                textBox2.Text = rowGrid.Cells[1].Value.ToString();
                textBox3.Text = rowGrid.Cells[2].Value.ToString();
                textBox4.Text = rowGrid.Cells[3].Value.ToString();
            }
            catch (Exception) { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ClienteService _ClienteService = new ClienteService();

            Cliente _Cliente = new Cliente();

            _Cliente.Id = int.Parse(textBox1.Text);
            _Cliente.Nome = textBox2.Text;
            _Cliente.RG = textBox3.Text;
            _Cliente.CPF = textBox4.Text;

            _ClienteService.AlteraCliente(_Cliente);

            Form1_Load(sender, e);
        }
    }
}
